﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media.Animation;

using MissedMemo.MeatAndCheeseDisc.OrderEntryService.OrderProcessing;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public partial class viewOrderDetails : UserControl
    {
        // Order items are not unique (2 Large Veggie pizzas...). This flag
        // signifies editing of an existing item (versus ADDING a new one)
        private bool _editingExistingItemValues;

        private GridLength _origImageColumnWidth;


        public viewOrderDetails()
        {
            InitializeComponent();

            DataContextChanged += OnDataContextChanged;

            _origImageColumnWidth = columnImages.Width;
        }


        void OnDataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            Order order = e.NewValue as Order;
            if( order != null )
            {
                // Set hosted WinForms control value (WinForms controls can't use WPF binding)
                datepickerOrderExpected.Value = order.DateTimeOrderExpectedComplete;

                // Ensure view always opens with edit item container hidden
                columnImages.Width = _origImageColumnWidth;
                BeginStoryboard( (Storyboard)FindResource( "InitEditViewContainerState" ) );
            }
        }


        private void btnAddPizza_Click( object sender, System.Windows.Input.MouseButtonEventArgs e )
        {
            Edit( new Pizza() );
        }

        private void btnAddDrink_Click( object sender, System.Windows.Input.MouseButtonEventArgs e )
        {
            Edit( new Drink() );
        }

        private void btnAddDessert_Click( object sender, System.Windows.Input.MouseButtonEventArgs e )
        {
            Edit( new Dessert() );
        }


        private void Edit( OrderItem item )
        {
            EditItemViewContainer.DataContext = item;

            if( item is Pizza )
            {
                PizzaEditView.Visibility = Visibility.Visible;
                DrinkEditView.Visibility = Visibility.Hidden;
                DessertEditView.Visibility = Visibility.Hidden;
            }
            else if( item is Drink )
            {
                PizzaEditView.Visibility = Visibility.Hidden;
                DrinkEditView.Visibility = Visibility.Visible;
                DessertEditView.Visibility = Visibility.Hidden;
            }

            else if( item is Dessert )
            {
                PizzaEditView.Visibility = Visibility.Hidden;
                DrinkEditView.Visibility = Visibility.Hidden;
                DessertEditView.Visibility = Visibility.Visible;
            }

            Storyboard animation = (Storyboard)FindResource( "DisplayEditViewContainer" );

            animation.Completed += ( sender, args ) =>
                columnImages.Width = new GridLength( 0 );

            BeginStoryboard( animation );
        }


        // Change values of an EXISTING order item
        private void listItems_DoubleClick( object sender, MouseButtonEventArgs e )
        {
            OrderItem item = listOrderItems.SelectedItems[0] as OrderItem;

            if( item != null )
            {
                // Clone simplifies CANCELLING DataContext edits
                Edit( item.Clone() );
                _editingExistingItemValues = true;
            }
        }


        private void btnOK_Click( object sender, RoutedEventArgs e )
        {
            Order order = DataContext as Order;
            OrderItem editedItem = EditItemViewContainer.DataContext as OrderItem;

            if( order != null && editedItem != null )
            {
                editedItem.UpdatePrice();

                if( !_editingExistingItemValues ) // Add NEW order item
                {
                    order.Items.Add( editedItem );
                }
                else // Replace EXISTING order item with edited version 
                {
                    int origItemIndex = listOrderItems.SelectedIndex;
                    order.Items[origItemIndex] = editedItem;
                }
            }

            CloseEditView();
        }


        private void btnCancel_Click( object sender, RoutedEventArgs e )
        {
            CloseEditView();
        }


        private void CloseEditView()
        {
            columnImages.Width = _origImageColumnWidth;
            BeginStoryboard( (Storyboard)FindResource( "HideEditViewContainer" ) );

            // Important! (eliminates accidental property resets)
            EditItemViewContainer.DataContext = null;

            _editingExistingItemValues = false;
        }


        private void btnDeleteItem_Click( object sender, RoutedEventArgs e )
        {
            Order order = DataContext as Order;

            if( order != null )
            {
                OrderItem item = ( sender as FrameworkElement ).DataContext as OrderItem;

                if( item != null )
                {
                    order.Items.Remove( item );
                }
            }
        }

        
        // Expected completion DateTime (or scheduled FUTURE delivery)
        
        private void OnEditDateTimeOrderExpected( object sender, System.EventArgs e )
        {
            Order order = DataContext as Order;

            if( order != null )
            {
                order.DateTimeOrderExpectedComplete = datepickerOrderExpected.Value;
            }
        }
    }
}
